package model.input;

import java.util.ArrayList;
import java.util.List;

import exceptions.CommandExecutionFail;
import exceptions.CommandInputNotValid;
import exceptions.TooManyWords;

import view.input.InputWords;


public class CommandInput extends Input{
	
	List<String> availableThings;
	private String horse = "horse";
	private String ogre = "ogre";
	private String man = "person";
	private String person = "ogre";
	private String knight = "knight";
	private String lord = "lord";
	
	
	private String Fyr = "Fyr";
	private String Saul = "Saul";
	private String Aaron = "Aaron";
	private String Amos = "Amos";
	

	private String north = "NORTH";
	private String south = "SOUTH";
	private String east = "EAST";
	private String west = "WEST";
	private String northeast = "northeast";
	private String northwest = "northwest";
	private String southeast = "southeast";
	private String southwest = "southwest";
	
	List<String> availableCommands = new ArrayList<String>();
	List<String> availableItems;
	private String give="GIVE";
	private String mount="MOUNT";
	private String talkTo="TALKTO";
	private String look="LOOK";
	private String go="GO";

	public CommandInput(){
		availableThings = new ArrayList<String>();
		availableItems=new ArrayList<String>();
		
		availableThings.add(horse);
		availableThings.add(ogre);
		availableThings.add(man);
		availableThings.add(person);
		availableThings.add(knight);
		availableThings.add(lord);
		availableThings.add(Fyr);
		availableThings.add(Saul);
		availableThings.add(north);
		availableThings.add(south);
		availableThings.add(east);
		availableThings.add(west);
		availableThings.add("LUKE");
		availableThings.add("SAUL");
	
		availableItems.add("necklace");
		
		availableCommands.add(give);
		availableCommands.add(mount);
		availableCommands.add(look);
		availableCommands.add(go);
		availableCommands.add(talkTo);
		availableCommands.add("O");
		availableCommands.add("OBJECTIVES");
		availableCommands.add("CLOSE");
		availableCommands.add("OPEN");
		availableThings.add("DOOR");
	
	}
	
	
	public InputWords getCommandWords(String input) throws CommandExecutionFail, CommandInputNotValid, TooManyWords
	{
		InputWords convWords= new InputWords();
		
		input=input.toUpperCase();
		//String input=read();
		//System.out.println(input);

		//quick hack
		if(input.contains("TALK TO")) input = "TALKTO" + input.substring(7);
		
		String[] words = input.split(" ");
		if( words.length > 3 )
		{
			throw new TooManyWords();			
		}
	
		for( String s : words )
		{
			convWords.add(s);
		}
		
		if((convWords.getWord1().equals("TALK")||convWords.getWord1().equals("SPEAK"))&&convWords.getWord2().equals("TO")){
			convWords.setWord1("TALKTO");
			convWords.setWord2(convWords.getWord3());

			}
			
		if(convWords.getWord1().equals("PICK")&&convWords.getWord2().equals("UP")){
			convWords.setWord1("PICKUP");
			convWords.setWord2(convWords.getWord3());

			}
		if( words.length == 3 ){
			for(String commandType: availableCommands){
				if(convWords.getWord1().equals(commandType)){
					for(String thing: availableThings){
						if(convWords.getWord2().equals(thing)||convWords.getWord3().equals(thing))
							for(String item:availableItems){
								if(convWords.getWord3().equals(item)){
									return convWords;
								}
							}
						throw new CommandExecutionFail(convWords.getWord2());
					}
					throw new CommandExecutionFail(convWords.getWord2());
				}
			}
			throw new CommandInputNotValid();
		}
		
		
		if( words.length == 2 ){
			for(String commandType: availableCommands){
				if(convWords.getWord1().equals(commandType)){
					for(String thing: availableThings){
						if(convWords.getWord2().equals(thing))
							return convWords;
					}
					for(String thing: availableItems){
						if(convWords.getWord2().equals(thing))
							return convWords;
					}
					throw new CommandExecutionFail(convWords.getWord2());
				}
			}
			throw new CommandInputNotValid();
		}
		if( words.length == 1 ){
			for(String commandType: availableCommands){
				if(convWords.getWord1().equals(commandType)){
					return convWords;
				}
			}
		}
			
		throw new CommandInputNotValid();
	}
	

	

}
